<?php

namespace Modules\Buy\Entities;

use Illuminate\Database\Eloquent\Model;

class Good extends Model
{
    protected $guarded = [];
//    商品对货品是一对多
    public function huowulist(){
        return $this->hasMany (Huowu::class);
    }

//    商品对品牌是多对一
    public function pinpai()
    {
        return $this->belongsTo(Brand::class,'brand_id','id');
    }


//    获得商品详情数据
    public function shopAttr($good)
    {
//        获得商品的顶级属性
        $attr = Arr::whereIn('id',explode(',',$good['can_id']))->get();
//        dd($attr);
//        获取当前商品的所有货品数据的属性组合
        $huowuattr =   Huowu::where('good_id',$good['id'])->pluck('attr');
//        dd($huowuattr);
//        定义一个空数组，用来合并属性
        $kong = [];
        foreach ($huowuattr as $v){
            $kong = array_merge($kong,explode(',',$v));
        }
//        dd($kong);
        $kong = array_unique($kong);
//        找到顶级属性再把子属性找到，方便商品循环判断
        foreach ($attr as $k=>$v){
            $attr[$k]['_data'] = Arr::where('pid',$v['id'])->whereIn('id',$kong)->get();
        }
//        dd($attr->toArray());

        return $attr;
    }

}
